<?php
/**
 * 
 * @author spring
 * @version 2014-6-7 上午11:11:42
 */
class  Rate extends Basic {
	
	/**
	 * 
	 * Description 添加汇率表记录
	 * @param field_type  bare_field_name
	 * @param array $condition
	 * @return int 返回影响行数
	 */
	public static function insert_rate($condition){
		list($insert_id, $total_rows) = DB::insert('rates',
				array('rate1','rate2','rate_time','name'))
				->values(array($condition['rate1'],$condition['rate2'],date('Y-m-d H:i:s'),$condition['name']))
						->execute('newerp');
				return   $total_rows;
		
	}
	
	 /**
    * 
    * Description 查询条件控制
    * @param field_type  bare_field_name
    * @param Kohana_Database_Query $pre
    * @param array $condition 查询条件
    * @param string $flag  为true就执行排序 
    * @param String $tablename 表名
    * @return Kohana_Database_Query 
    */
	public static function getListInfoCondition($pre,$condition,$flag=true,$tablename){
		if(isset($condition['name'])&&!empty($condition['name'])){
			$condition1=array('name','=',$condition['name']);
			$pre=self::condition($pre,$condition1);
		}
		if(isset($condition['start_time'])&&!empty($condition['start_time'])){
			$condition1=array('rate_time','>=',$condition['start_time']);
			$pre=self::condition($pre,$condition1);
		}
		if(isset($condition['end_time'])&&!empty($condition['end_time'])){
			$time=date('Y-m-d', strtotime ("+1 day", strtotime($condition['end_time'])));
			$condition1=array('rate_time','<',$time);
			$pre=self::condition($pre,$condition1);
		}
		if(isset($condition['down_temple'])&&!empty($condition['down_temple'])){
			$condition1=array('rate_time','=','2014-05-01');
			$pre=self::condition($pre,$condition1);
		}
		
		return $pre;
	}
	/**
	 * 
	 * Description 修改汇率
	 * @param field_type  bare_field_name
	 * @param array $condition
	 * @return int 影响行数
	 */
	 public  static function editRate($condition){
	 	$total_rows=DB::update('rates')
	 	->set(array('rate1'=>$condition['rate1'],'rate2'=>$condition['rate2']))
	 	->where('id','=',$condition['id'])
	 	->execute('newerp');
	 	return $total_rows;
	 }
	 
	 
	 public static function check_rate_name($name){
	 	
	 	return DB::select('currency_id')->from('erp_currency_info')->where('currency_type','=',$name)->execute('readonlyerp')->get('currency_id',0);
	 }
	 
	 
	 public static  function check_saleaccount($saleaccount){
	 	return DB::select('id')->from('ebay_account')->where('ebay_account','=',$saleaccount)->execute('readonlyerp')->get('id',0);
	 }
	 
	 
	 public static function check_rate_upload($data){
	 	$num = count($data);
	 	for($i=2;$i<=$num;$i++){
	 		$name=trim($data[$i]['A']);
	 		$rate1=trim($data[$i]['B']);
	 		$rate2=trim($data[$i]['C']);
	 		if(!isset($name)||empty($name)){
	 			self::alert('你输入的第'.$i.'行币种不能为空');
	 		}
	 		if(!isset($rate1)||empty($rate1)){
	 			self::alert('你输入的第'.$i.'行人民币汇率不能为空');
	 		}
	 		if(!isset($rate2)||empty($rate2)){
	 			self::alert('你输入的第'.$i.'行美元汇率不能为空');
	 		}
	 		if(!preg_match('/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/',$rate1)){
	 			self::alert('你输入的第'.$i.'行人民币汇率不符合规则');
	 		}
	 		if(!preg_match('/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/',$rate2)){
	 			self::alert('你输入的第'.$i.'行美元汇率不符合规则');
	 		}
	 	}
	 
	 }
	 
	 public static function do_rate_upload($data){
	 	$num = count($data);
	 	$userid = User::active_user()->id;
	 	$lines=array();
	 	for($i=2;$i<=$num;$i++){
	 		$name=trim($data[$i]['A']);
	 		$rate1=trim($data[$i]['B']);
	 		$rate2=trim($data[$i]['C']);
	 		$condition=array('name'=>$name,'rate1'=>$rate1,'rate2'=>$rate2);
	 		if(!self::insert_rate($condition)){
	 			$lines[]=$num;
	 		}
	 	}
	 	return $lines;
	 }
}
